import BaseSingleClass from "@/BaseSingleClass";
import GameModel from "@/game/model/GameModel";
import {service as request} from "@/utils/request"

export default class ApiHelper extends BaseSingleClass{
    /**
     * 检查验证码是否正确
     * @param code 验证码
     * @returns 
     */
    checkCode(code: string) {
    //throw new Error('Method not implemented.');
        return new Promise<any>((resolve,reject)=>{
            request.post('/check_code',{code:code}).then((res:any)=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)})
    }
    /**
     * 登录
     * @param account 
     * @param password 
     * @param code 
     * @returns 
     */
    login(account:string,password:string,code:string):Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/login',{account:account,password:password,code:code}).then((res:any)=>{
                if(res.status==200){
                    if(res.data.statusCode=='success'){
                        resolve(res.data.data)
                    }else{
                        reject(res.data.message)
                    }
                }
            }).catch(reject)
        })
    }
    /**
     * 获取盐值
     * @returns Promise<any>
     */
    getSalt(name:string):Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/salt',{user_name:name}).then((res:any)=>{
                //console.log(res)
                if(res.status==200){
                    if(res.data.statusCode=='success'){
                        resolve(res.data.data)
                    }else{
                        reject(res.data.message)
                    }
                }
            }).catch(reject)
        })
    }
    /**
     * 获取用户信息
     * @returns 
     */
    getUserInfo():Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/user').then((res:any)=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }else{
                    reject(null)
                }
            }).catch(reject)
        })
        //return request.post('/user')
    }
    /**
     * 获取游戏信息
     * @returns Promise<any>
     */
    getGameInfo():Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/game_info').then((res:any)=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
        //return request.post('/game_info')
    }
    /**
     * 更新游戏排序
     * @param list 
     * @returns 
     */
    updateGameOrder(list:any[]):Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/update_game_order',{list:list}).then((res:any)=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
        //return request.post('/update_game_order')
    }
    getGameConfig(id:string):Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.get('/get_game_config?id='+id).then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
        //return request.post('/game_config')
    }
    /**
     * 获取所有图片
     * @returns 
     */
    getImages():Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.get('/get_image_list').then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
        //return request.post('/game_config')
    }
    /**
     * 获取所有动画
     * @returns 
     */
    getAnimations():Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.get('/get_animation_list').then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
    }

    /**
     * 删除图片
     * @param id 
     * @returns 
     */
    deleteImage(id:number):Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/delete_image',{id:id}).then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
        //return request.post('/game_config')
    }
    addAnimation(data:any):Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/add_animation',data).then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
           }).catch(reject)
        })
    }
    /**
     * 删除动画
     * @param id 
     * @returns 
     */
    deleteAnimation(id:number):Promise<any>{
        return new Promise<any>((resolve,reject)=>{
            request.post('/delete_animation',{id:id}).then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
    }
    addGame(model:GameModel):Promise<any>{
        return new Promise((resolve,reject)=>{
            request.post('/create_game',model).then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            }).catch(reject)
        })
    }
    removeGame(id:number):Promise<any>{
        return new Promise((resolve,reject)=>{
            request.post('/remove_game',{
                id:id
            }).then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            })
        })
    }
    saveGame(id:string,config:string):Promise<any>{
        return new Promise((resolve,reject)=>{
            request.post('/save_game',{
                id:id,
                config:config
            }).then(res=>{
                if(res.status==200){
                    if(res.data.statusCode!='success'){
                        reject(res.data.message)
                    }else{
                        resolve(res.data.data)
                    }
                }
            })
        })
    }
}